package com.qeeyou.apps.android.craftsman.activity.publish;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.qeeyou.apps.android.craftsman.R;

public class WorkingDayActivity extends Activity {

    private Button btnTime1;
    private Button btnTime2;
    private Button btnTime3;
    private Button btnTime4;
    private Button btnTime5;
    private Button btnTime6;
    private Button btnTime7;
    private Button btnCancel;                   //取消按钮
    private Button btnConfirm;                  //确定按钮
    private String workingDay;                  //工期

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_working_day);

        getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        setView();
        setEvent();

    }

    private void setView() {
        btnTime1 = this.findViewById(R.id.btnTime14WorkingDay);
        btnTime2 = this.findViewById(R.id.btnTime24WorkingDay);
        btnTime3 = this.findViewById(R.id.btnTime34WorkingDay);
        btnTime4 = this.findViewById(R.id.btnTime44WorkingDay);
        btnTime5 = this.findViewById(R.id.btnTime54WorkingDay);
        btnTime6 = this.findViewById(R.id.btnTime64WorkingDay);
        btnTime7 = this.findViewById(R.id.btnTime74WorkingDay);
        btnCancel = (Button) this.findViewById(R.id.btnCancel4WorkingDay);
        btnConfirm = (Button) this.findViewById(R.id.btnConfirm4WorkingDay);
    }

    private void setEvent() {
        btnCancel.setOnClickListener(onCancelClickListener);
        btnConfirm.setOnClickListener(onConfirmClickListener);
        btnTime1.setOnClickListener(onTimeClickListener);
        btnTime2.setOnClickListener(onTimeClickListener);
        btnTime3.setOnClickListener(onTimeClickListener);
        btnTime4.setOnClickListener(onTimeClickListener);
        btnTime5.setOnClickListener(onTimeClickListener);
        btnTime6.setOnClickListener(onTimeClickListener);
        btnTime7.setOnClickListener(onTimeClickListener);
    }

    private View.OnClickListener onTimeClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            btnTime1.setSelected(false);
            btnTime2.setSelected(false);
            btnTime3.setSelected(false);
            btnTime4.setSelected(false);
            btnTime5.setSelected(false);
            btnTime6.setSelected(false);
            btnTime7.setSelected(false);
            if(view == btnTime1) {
                workingDay = "半个月";
                btnTime1.setSelected(true);
            } else if(view == btnTime2) {
                workingDay = "一个月";
                btnTime2.setSelected(true);
            } else if(view == btnTime3) {
                workingDay = "三个月";
                btnTime3.setSelected(true);
            } else if(view == btnTime4) {
                workingDay = "半年";
                btnTime4.setSelected(true);
            } else if(view == btnTime5) {
                workingDay = "一年";
                btnTime5.setSelected(true);
            } else if(view == btnTime6) {
                workingDay = "两年";
                btnTime6.setSelected(true);
            } else {
                workingDay = "三年";
                btnTime7.setSelected(true);
            }
        }
    };

    //返回按钮
    private View.OnClickListener onCancelClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    WorkingDayActivity.this.finish();
                }
            }, 500);
        }
    };

    //确定按钮
    private View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(workingDay != null) {
                Intent intent = new Intent();
                intent.putExtra("working_day", workingDay);
                WorkingDayActivity.this.setResult(RESULT_OK, intent);
            }
            WorkingDayActivity.this.finish();
        }
    };

}
